package com.yxw.yxnet_cd_center.customer_service.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxw.yxnet_cd_center.common.constant.CommonConstant;
import com.yxw.yxnet_cd_center.common.enums.NoticeTypeEnum;
import com.yxw.yxnet_cd_center.customer_service.dto.SendNoticeViewDTO;
import com.yxw.yxnet_cd_center.customer_service.entity.YxCsNoticeCircleEntity;
import com.yxw.yxnet_cd_center.customer_service.mapper.NoticeCircleMapper;
import com.yxw.yxnet_cd_center.customer_service.service.NoticeCircleService;
import com.yxw.yxnet_cd_center.customer_service.service.NoticeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;


/**
 * 消息通知轮询服务
 * @author liuxin
 * @date 2023.10.10
 */
@Slf4j
@Service
public class NoticeCircleServiceImpl extends ServiceImpl<NoticeCircleMapper, YxCsNoticeCircleEntity> implements NoticeCircleService {


    /**
     * 保存消息轮询相关数据
     * @param dto
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveNoticeCircle(SendNoticeViewDTO dto) {
        NoticeTypeEnum type = dto.getType();
        LambdaQueryWrapper<YxCsNoticeCircleEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(true,YxCsNoticeCircleEntity::getBussId,dto.getBussId())
                    .eq(true,YxCsNoticeCircleEntity::getNoticeType,type.getCodeType());
        YxCsNoticeCircleEntity entity = baseMapper.selectOne(queryWrapper);
        if(null==entity){
            entity=new YxCsNoticeCircleEntity();
            BeanUtil.copyProperties(dto,entity);
            //轮询次数
            Integer cycleTime = dto.getCycleTime()* 60;
            Integer cycleFreq = dto.getCycleFreq();
            Integer totalNum=cycleTime % cycleFreq==0?cycleTime /cycleFreq-1:cycleTime /cycleFreq;
            entity.setTotalNum(totalNum);
            entity.setNoticeType(type.getCodeType());
            entity.setCreateTime(new Date());
            baseMapper.insert(entity);
        }
    }
}
